home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 2.toast / pc / sample code / sound / sndplaydoublebuffer / _source / mungebuffer.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-09-28  |  4.7 KB  |  157 lines

  1. /*
  2.     File:        MungeBuffer.c
  3.  
  4.     Contains:    Routines demonstrating how to manipulate buffers of WAVE data to get them to play correctly.
  5.  
  6.     Written by: Mark Cookson    
  7.  
  8.     Copyright:    Copyright © 1996-1999 by Apple Computer, Inc., All Rights Reserved.
  9.  
  10.                 You may incorporate this Apple sample source code into your program(s) without
  11.                 restriction. This Apple sample source code has been provided "AS IS" and the
  12.                 responsibility for its operation is yours. You are not permitted to redistribute
  13.                 this Apple sample source code as "Apple sample source code" after having made
  14.                 changes. If you're going to re-distribute the source, we require that you make
  15.                 it clear in the source that the code was descended from Apple sample source
  16.                 code, but that you've made changes.
  17.  
  18.     Change History (most recent first):
  19.                 8/31/1999    Karl Groethe    Updated for Metrowerks Codewarror Pro 2.1
  20.                 
  21.  
  22. */
  23. #include "MungeBuffer.h"
  24.  
  25. /* This PowerPC assembly routine is used to make a buffer of WAVE sound
  26.    data play as if it was a buffer of a AIFF sound data.
  27.  
  28.    All this really means is that you have to do the endian conversion without
  29.    rearanging the channels if the sound is stereo.
  30.  
  31.    The buffer addressed is passed in register r3 and the count is passed in r4.
  32. */
  33. #ifdef ASM    // Should we use the assembly versions or the C versions of the buffer munging routines?
  34.  
  35. #ifdef powerc
  36.  
  37. asm void Endian16BitBuffer (Ptr buf, unsigned long count)
  38. {
  39. #pragma unused (buf, count)
  40.         b        test                        // Make sure count is good
  41. more:    subi    r4, r4, 0x04                // Move pointer back 4 bytes to next long
  42.         lwzx    r5, r3, r4                    // Load the long
  43.         rlwinm    r5, r5, 0x10, 0x00, 0x1F    // Rotate long left 16 bits
  44.         stwbrx    r5, r3, r4                    // Write it back out byte reversed
  45. test:    cmpwi    r4, 0x03                    // Are we done yet?
  46.         bgt        more                        // Go back to process more longs
  47.         blr                                    // We're outta here
  48. }
  49.  
  50. /* These are the coresponding 68K versions of the above PowerPC assembly buffer munging routines.
  51.  
  52.    These calls will be called if the target is a 68K based Mac.
  53.  
  54.    The buffer address is passed as the second value (from the top) of the stack, and the count
  55.    is the third value (from the top) of the stack.
  56. */
  57.  
  58. #else //generating 68K
  59.  
  60. asm void Endian16BitBuffer (Ptr buf, unsigned long count)
  61. {
  62.         movea.l    0x04(sp), a0                // Put address of buffer in a0
  63.         move.l    0x08(sp), d0                // Put count in d0
  64.         lsr.l    #0x02, d0                    // Divide count by 4
  65.         bra        test                        // Make sure count is good
  66. more:    move.l    (a0), d1                    // Load the long
  67.         ror.w    #0x08, d1                    // Rotate bottom 16 bits
  68.         swap    d1                            // Swap upper and lower words of d1
  69.         ror.w    #0x08, d1                    // Rotate bottom 16 bits (used to be top bits)
  70.         swap    d1                            // Swap upper and lower words of d1
  71.         move.l    d1, (a0)+                    // Store the result
  72. test:    dbra    d0, more                    // Go back and process more longs
  73.         rts                                    // We're outta here
  74. }
  75.  
  76. #endif
  77.  
  78. #else
  79.  
  80. /* Use C functions instead of assembly functions.
  81.    To use these functions, undefine ASM in MungeBuffer.h.
  82. */
  83.  
  84. void Endian16BitBuffer (Ptr buf, unsigned long count)
  85. {
  86.     unsigned long    i;
  87.     unsigned char    tempChar;
  88.  
  89.     for (i = kStartOfBuffer; i < count; i += sizeof (short)) {
  90.         tempChar = (buf)[i];
  91.         (buf)[i] = (buf)[i+1];
  92.         (buf)[i+1] = tempChar;
  93.     }
  94. }
  95.  
  96. #endif
  97.  
  98. void ReverseMono8BitBuffer (const Ptr buf, unsigned long count)
  99. {
  100.     unsigned char    *endBuffer;
  101.     unsigned char    *startBuffer = (unsigned char *)buf;
  102.     unsigned char    saveByte;
  103.     long            i;
  104.  
  105.     endBuffer = ((unsigned char *)buf + count) - 1;
  106.     for (i = (count / (sizeof (char) * kHalfOfBuffer)); i >= kStartOfBuffer; --i) {
  107.         saveByte = *endBuffer;
  108.         *endBuffer-- = *startBuffer;
  109.         *startBuffer++ = saveByte;
  110.     }
  111. }
  112.  
  113. void ReverseStereo8BitBuffer (const Ptr buf, unsigned long count)
  114. {
  115.     unsigned short    *endBuffer;
  116.     unsigned short    *startBuffer = (unsigned short *)buf;
  117.     unsigned short    saveShort;
  118.     long            i;
  119.  
  120.     endBuffer = (unsigned short *)(buf + count) - 1;
  121.     for (i = (count / (sizeof (short) * kHalfOfBuffer)); i >= kStartOfBuffer; --i) {
  122.         saveShort = *endBuffer;
  123.         *endBuffer-- = *startBuffer;
  124.         *startBuffer++ = saveShort;
  125.     }
  126. }
  127.  
  128. void ReverseMono16BitBuffer (const Ptr buf, unsigned long count)
  129. {
  130.     unsigned short    *endBuffer;
  131.     unsigned short    *startBuffer = (unsigned short *)buf;
  132.     unsigned short    saveShort;
  133.     long            i;
  134.  
  135.     endBuffer = (unsigned short *)(buf + count) - 1;
  136.     for (i = (count / (sizeof (short) * kHalfOfBuffer)); i >= kStartOfBuffer; --i) {
  137.         saveShort = *endBuffer;
  138.         *endBuffer-- = *startBuffer;
  139.         *startBuffer++ = saveShort;
  140.     }
  141. }
  142.  
  143. void ReverseStereo16BitBuffer (const Ptr buf, unsigned long count)
  144. {
  145.     unsigned long    *endBuffer;
  146.     unsigned long    *startBuffer = (unsigned long *)buf;
  147.     unsigned long    saveLong;
  148.     long            i;
  149.  
  150.     endBuffer = (unsigned long *)(buf + count) - 1;
  151.     for (i = (count / (sizeof (long) * kHalfOfBuffer)); i >= kStartOfBuffer; --i) {
  152.         saveLong = *endBuffer;
  153.         *endBuffer-- = *startBuffer;
  154.         *startBuffer++ = saveLong;
  155.     }
  156. }
  157.